﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Diagnostics;
using System.Net;

namespace wangye
{
    public partial class Form_main : Form
    {
        public Form_main()
        {
            InitializeComponent();
        }

        /// <summary>
        /// tian jia ti mu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// tian jia lei bie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// dao ru
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            try
            {
                //string strFileName = (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ".log").Replace(":", "");
                StreamWriter sw4 = new StreamWriter("分类\\" + "1.log", false, Encoding.Default);
                sw4.WriteLine("");
                sw4.Flush();
                sw4.Close();
                int chongfu = 0;
                int daoru = 0;

                //da kai wen jian 
                string str_file = "";
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.Filter = "xsl数据文件(*.csv)|*.*";
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    str_file = openFileDialog1.FileName;

                }


                if (string.IsNullOrEmpty(str_file)) { return; }
                //读取所有csv文件 手机号记入数组
                ArrayList al = new ArrayList();
                DirectoryInfo di = new DirectoryInfo("分类");
                FileInfo[] f1 = di.GetFiles("*.csv", SearchOption.AllDirectories);
                foreach (FileInfo f2 in f1)
                {
                    StreamReader sr = f2.OpenText();
                    sr.ReadLine();
                    string xinxi = sr.ReadLine();
                    if (string.IsNullOrEmpty(xinxi)) { continue; }
                    string[] xin = xinxi.Split(',');
                    string x = xin[4];
                    al.Add(x);
                    sr.Close();

                }

                //读取导入文件
                StreamReader sr1 = new StreamReader(str_file, Encoding.Default);
                string title = sr1.ReadLine();
                while (!sr1.EndOfStream)
                {
                    string xinxi = sr1.ReadLine();
                    string[] xin = xinxi.Split(',');
                    string x = xin[4];

                    if (al.Contains(x))
                    {
                        //MessageBox.Show(xin[4] + "手机号重复");
                        StreamWriter sw1 = new StreamWriter("分类\\" + "1.log", true, Encoding.Default);
                        sw1.WriteLine(xin[0] + " " + xin[4] + "手机号重复");
                        chongfu++;
                        sw1.Flush();
                        sw1.Close();

                    }
                    else
                    {
                        StreamWriter sw = new StreamWriter("分类\\" + xin[0] + ".csv", false, Encoding.Default);
                        sw.WriteLine(title);
                        sw.WriteLine(xinxi);
                        daoru++;
                        sw.Flush();
                        sw.Close();


                        StreamWriter sw1 = new StreamWriter("分类\\" + "1.log", true, Encoding.Default);
                        sw1.WriteLine(xin[0] + " " + xin[4] + "导入");
                        sw1.Flush();
                        sw1.Close();
                    }


                }

                sr1.Close();

                StreamWriter sw2 = new StreamWriter("分类\\" + "1.log", true, Encoding.Default);
                sw2.WriteLine("");
                sw2.WriteLine("导入：" + daoru.ToString());
                sw2.WriteLine("重复：" + chongfu.ToString());
                sw2.Flush();
                sw2.Close();

                //hou xu
                load_fenlei();
                Process.Start("分类\\1.log");

                //StreamReader s2 = new StreamReader("分类\\1.log", Encoding.Default);


                //if (string.IsNullOrEmpty(s2.ReadToEnd()))
                //{
                //    MessageBox.Show("导入完成");
                //}
                //else
                //{
                //    Process.Start("分类\\1.log");
                //}
                //s2.Close();
            }
            catch (Exception ee)
            {

                MessageBox.Show(ee.ToString());
            }

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {

        }


        /// <summary>
        /// liu lan ti mu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            load_fenlei();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.Tag == "r")
            {
                load_fenlei();

            }

            else if (treeView1.SelectedNode.Tag == "d")
            {
                LoadDir();
            }

            else if (treeView1.SelectedNode.Tag == "f")
            {


                LoadText();


            }
        }

        private void LoadText()
        {
            try
            {
                string path = treeView1.SelectedNode.FullPath;
                StreamReader sr = new StreamReader(path, Encoding.Default);
                string temp = sr.ReadToEnd();
                sr.Close();
                this.richTextBox1.Text = "";
                this.richTextBox1.Text = temp;
                this.richTextBox1.Tag = path;
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
        }

        private void LoadD(TreeNode tn)
        {

            tn.Nodes.Clear();
            string path = tn.FullPath;
            string[] dirs = Directory.GetFiles(path);
            foreach (string s in dirs)
            {
                TreeNode tnNew = new TreeNode(Path.GetFileName(s));
                tnNew.Tag = "f";
                tnNew.ImageKey = "txt.bmp";
                tnNew.SelectedImageKey = "txt.bmp";
                tn.Nodes.Add(tnNew);
                //treeView1.SelectedNode = tnNew;
            }

            tn.ExpandAll();
        }

        private void LoadDir()
        {
            LoadD(treeView1.SelectedNode);
            //treeView1.SelectedNode.Nodes.Clear();
            //string path = treeView1.SelectedNode.FullPath;
            //string[] dirs = Directory.GetFiles(path);
            //foreach (string s in dirs)
            //{
            //    TreeNode tnNew = new TreeNode(Path.GetFileName(s));
            //    tnNew.Tag = "f";
            //    tnNew.ImageKey = "txt.bmp";
            //    tnNew.SelectedImageKey = "txt.bmp";
            //    treeView1.SelectedNode.Nodes.Add(tnNew);
            //    //treeView1.SelectedNode = tnNew;
            //}

            //treeView1.SelectedNode.ExpandAll();
        }
        /// <summary>
        /// tian jia xiu gai ti mu 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click_1(object sender, EventArgs e)
        {


        }

        private void RefreshColor(TreeNode tn)
        {
            tn.ForeColor = SystemColors.ControlText;
            foreach (TreeNode tnOne in tn.Nodes)
            {
                RefreshColor(tnOne);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (this.toolStripTextBox1.Text.Trim() == "")
            { return; }
            //if (treeView1.SelectedNode == null) { return; }
            foreach (TreeNode tn in treeView1.Nodes)
            {
                RefreshColor(tn);
            }
            //treeView1.SelectedNode.ForeColor = Color.Blue;
            treeView1.Nodes[0].ForeColor = Color.Blue;
            FindFile(treeView1.Nodes[0], this.toolStripTextBox1.Text.Trim());






        }

        private void FindFile(TreeNode treeNode, string strSearch)
        {

            foreach (TreeNode subNode in treeNode.Nodes)
            {
                if (subNode.Text.ToUpper().IndexOf(strSearch.ToUpper()) != -1)
                {
                    subNode.ForeColor = Color.Red;
                    subNode.Parent.Expand();

                }
                FindFile(subNode, strSearch);

            }

            //treeNode.Nodes.Clear();
            //DirectoryInfo diRoot = new DirectoryInfo(treeNode.FullPath);
            //FileInfo[] theFiles = diRoot.GetFiles();
            //foreach (FileInfo theFile in theFiles)
            //{

            //    TreeNode subNode = new TreeNode(theFile.Name);
            //    subNode.Tag = "f";
            //    subNode.ImageKey = "txt.bmp";
            //    subNode.SelectedImageKey = "txt.bmp";
            //    if (subNode.Text.ToUpper().IndexOf(strSearch.ToUpper()) != -1)
            //    {
            //        subNode.ForeColor = Color.Red;

            //    }



            //    treeNode.Nodes.Add(subNode);


            //    if (subNode.Text.ToUpper().IndexOf(strSearch.ToUpper()) != -1)
            //    {
            //        subNode.ForeColor = Color.Red;
            //        subNode.Parent.Expand();
            //    }

            //}
        }

        private void load_fenlei()
        {


            treeView1.Nodes[0].Nodes.Clear();
            string path = "分类";
            string[] dirs = Directory.GetDirectories(Path.Combine(Application.StartupPath, path));
            foreach (string s in dirs)
            {
                TreeNode tnNew = new TreeNode(Path.GetFileName(s));
                tnNew.Tag = "d";
                tnNew.ImageKey = "dirClose.bmp";
                tnNew.SelectedImageKey = "dirClose.bmp";
                treeView1.Nodes[0].Nodes.Add(tnNew);

                LoadD(tnNew);
                //treeView1.SelectedNode = tnNew;
            }

            string[] files = Directory.GetFiles(path);
            foreach (string s in files)
            {
                TreeNode tnNew = new TreeNode(Path.GetFileName(s));
                tnNew.Tag = "f";
                tnNew.ImageKey = "txt.bmp";
                tnNew.SelectedImageKey = "txt.bmp";
                treeView1.Nodes[0].Nodes.Add(tnNew);
                //treeView1.SelectedNode = tnNew;
            }


            treeView1.Nodes[0].ExpandAll();


        }

        //private void load_feilei_file() {
        //    load_fenlei();


        //}


        private void Form_main_Load(object sender, EventArgs e)
        {
            Form_main.CheckForIllegalCrossThreadCalls = false;

            treeView1.Nodes.Clear();
            TreeNode treeNode = new TreeNode();
            treeNode.Text = "分类";
            treeNode.Tag = "r";
            treeNode.ImageKey = "dir.bmp";
            treeNode.SelectedImageKey = "dir.bmp";
            treeView1.Nodes.Add(treeNode);


            load_fenlei();


            Form_send.tmpCookies = new CookieCollection();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 建立分类ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string root = Application.StartupPath + "\\分类";

            string strFileName = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(":", "");
            Directory.CreateDirectory(Path.Combine(root, strFileName));

            TreeNode tnNew = new TreeNode(strFileName);
            tnNew.Tag = "d";
            treeView1.Nodes[0].Nodes.Add(tnNew);
            treeView1.SelectedNode = tnNew;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 建立联系人ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.treeView1.SelectedNode.Tag == "d")
            {
                string path = treeView1.SelectedNode.FullPath;
                string strFileName = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace(":", "");
                //File.Create(Path.Combine(path, strFileName + ".txt"));

                string temp = "url:\n\npost:\n\nmethod:\nrequest\n";
                StreamWriter sw = new StreamWriter(Path.Combine(path, strFileName + ".txt"), false, Encoding.Default);
                sw.Write(temp);
                sw.Flush();
                sw.Close();


                TreeNode tnNew = new TreeNode(strFileName + ".txt");
                tnNew.Tag = "f";
                tnNew.ImageKey = "txt.bmp";
                tnNew.SelectedImageKey = "txt.bmp";
                treeView1.SelectedNode.Nodes.Add(tnNew);
                treeView1.SelectedNode = tnNew;


            }
            else
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control == true)
            {
                if (e.KeyValue == 83)
                {
                    Encoding encode = Encoding.Default;
                    string temp = this.richTextBox1.Text;
                    StreamWriter sw = new StreamWriter(richTextBox1.Tag.ToString(), false, encode);
                    sw.Write(temp);
                    sw.Flush();
                    sw.Close();

                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyData == Keys.F2)
                {
                    StartRename();
                }


                else if (e.KeyData == Keys.Delete)
                {

                    this.richTextBox1.Text = "";
                    FileCtrl.PutRececal(treeView1.SelectedNode.FullPath.ToString());
                    treeView1.SelectedNode.Remove();

                }

            }


            catch (Exception ee)
            {

                MessageBox.Show(ee.ToString());
            }
        }
        /// <summary>
        /// Places the currently selected node in rename mode.
        /// </summary>
        public void StartRename()
        {
            try
            {
                if (this.treeView1.SelectedNode != null)
                {
                    treeView1.LabelEdit = true;
                    treeView1.SelectedNode.BeginEdit();

                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            try
            {
                if (e.Node.Text == e.Label || e.Label == null) { return; }

                if ((!e.CancelEdit) && (string.Compare(e.Label, e.Node.Text, false) != 0))
                {
                    if (e.Node.Tag.ToString() == "d")
                    {

                        DirectoryInfo d = new DirectoryInfo(e.Node.FullPath.ToString());
                        string strDir = d.FullName.Substring(0, d.FullName.LastIndexOf("\\") + 1) + e.Label;
                        d.MoveTo(strDir);


                        this.treeView1.SelectedNode = e.Node;
                        treeView1.LabelEdit = false;

                        //LoadDirAndFile(e.Node);
                        return;

                    }

                    if (e.Node.Tag.ToString() == "f")
                    {

                        string strFile = Path.Combine(Path.GetDirectoryName(e.Node.FullPath), e.Label);
                        this.MoveFile(e.Node.FullPath, strFile);
                        treeView1.LabelEdit = false;
                        e.Node.Text = e.Label;
                        this.treeView1.SelectedNode = e.Node;
                        LoadText();
                    }

                }





            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        public void MoveFile(string oldUrlName, string newUrlName)
        {
            if (System.IO.File.Exists(oldUrlName))
            {
                System.IO.File.Move(oldUrlName, newUrlName);

                System.IO.File.Delete(oldUrlName);
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Process.Start(treeView1.SelectedNode.FullPath);
            //if (this.treeView1.SelectedNode.Tag == "f")
            //{


            //}
            //else { 

            //}
        }




        private void treeView1_DragDrop(object sender, DragEventArgs e)
        {


            Point targetPoint = treeView1.PointToClient(new Point(e.X, e.Y));
            TreeNode targetNode = treeView1.GetNodeAt(targetPoint);
            TreeNode draggedNode = (TreeNode)e.Data.GetData(typeof(TreeNode));

            string newPath;
            string oldPath;

            if (draggedNode.Tag != "f") { return; }

            if (!draggedNode.Equals(targetNode))
            {
                if (e.Effect == DragDropEffects.Move)
                {


                    if (targetNode.Tag == "f")
                    {


                        oldPath = draggedNode.FullPath.ToString();
                        newPath = Path.Combine(targetNode.Parent.FullPath.ToString(), draggedNode.Text);



                        File.Move(oldPath, newPath);
                        draggedNode.Remove();
                        draggedNode.Tag = "f";


                        targetNode.Parent.Nodes.Add(draggedNode);
                        targetNode.Parent.Expand();

                    }

                    else
                    {


                        oldPath = draggedNode.FullPath.ToString();
                        newPath = Path.Combine(targetNode.FullPath.ToString(), draggedNode.Text);

                        File.Move(oldPath, newPath);
                        draggedNode.Remove();
                        draggedNode.Tag = "f";


                        targetNode.Nodes.Add(draggedNode);
                        targetNode.Parent.Expand();

                    }


                }


            }

        }

        private void treeView1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;
        }

        private void treeView1_ItemDrag(object sender, ItemDragEventArgs e)
        {
            try
            {

                if (e.Button == MouseButtons.Left)
                {
                    DoDragDrop(e.Item, DragDropEffects.Move);

                }

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }

        }

        /// <summary>
        /// dao chu
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click_2(object sender, EventArgs e)
        {
            try
            {
                string strFileName = ("备份\\" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ".csv").Replace(":", "");

                //FileStream fs = File.Create(strFile);

                string temp = "姓名,昵称,性别,QQ/TM帐号,常用手机,备用手机,小灵通,工作电话,家庭电话,其它电话,常用邮箱,备用邮箱,单位地址-邮编,单位地址-国家/地区,单位地址-省,单位地址-市,单位地址-街道,住宅地址-邮编,住宅地址-国家/地区,住宅地址-省,住宅地址-市,住宅地址-街道,其它地址-邮编,其它地址-国家/地区,其它地址-省,其它地址-市,其它地址-街道,公司名称,隶属行业,部门名称,职务名称,生日,简介与备注";
                StreamWriter sw = new StreamWriter(strFileName, false, Encoding.Default);
                sw.Write(temp);


                string path = "分类";
                string[] dirs = Directory.GetDirectories(Path.Combine(Application.StartupPath, path));
                foreach (string s in dirs)
                {
                    if (s.IndexOf("废") == -1)
                    {
                        string[] files1 = Directory.GetFiles(s);
                        foreach (string s1 in files1)
                        {
                            //StreamReader sr = new StreamReader(s1);
                            StreamReader sr = new StreamReader(s1, Encoding.Default);
                            sr.ReadLine();
                            string content = sr.ReadLine();
                            sw.WriteLine(content);
                        }

                    }
                }

                string[] files = Directory.GetFiles(path);
                foreach (string s in files)
                {
                    StreamReader sr = new StreamReader(s, Encoding.Default);
                    sr.ReadLine();
                    string content = sr.ReadLine();
                    sw.WriteLine(content);
                }


                sw.Flush();
                sw.Close();
                //MessageBox.Show("导出成功");
                Process.Start("备份");

            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }

        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 访问ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode tn = this.treeView1.SelectedNode;
            string file_path = tn.FullPath;
            StreamReader sr = new StreamReader(file_path);
            bool isurl = false;
            bool ispost = false;
            bool ismethod = false;
            bool isimg = false;
            bool isencoding = false;

            string url = "";
            string post = "";
            string method = "";
            string encoding="";
           

            while (!sr.EndOfStream)
            {
                string temp = sr.ReadLine();
                if (!string.IsNullOrEmpty(temp.Trim()))
                {
                    if (temp.IndexOf("url:") != -1)
                    {
                        isurl = true;
                    }
                    else if (temp.IndexOf("post:") != -1)
                    {
                        ispost = true;
                        isurl = false;
                    }
                    else if (temp.IndexOf("method:") != -1)
                    {
                        ismethod = true;
                        ispost = false;
                        isurl = false;
                    }
                    else if (temp.IndexOf("img:") != -1)
                    {
                        isimg = true;
                        
                    }
                    else if (temp.IndexOf("encoding:") != -1)
                    {
                        isencoding = true;

                    }
                    else if (isencoding)
                    {
                        encoding = temp.Trim();
                        isencoding = false;
                    }
                    

                    else if (isurl)
                    {
                        url = temp.Trim();
                        isurl = false;
                    }

                    
                    else if (ispost)
                    {
                        post += temp.Trim();
                    }

                   

                    else if(ismethod){
                        method = temp.Trim();
                        ismethod = false;
                    }

                   
                }

                


            }
            sr.Close();



            Form_send fs = new Form_send();
            
            fs.url = url;
            fs.post = post;
            fs.method = method;
            fs.isimg = isimg;
            fs.encoding = encoding;
            fs.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void toolStripButton2_Click_3(object sender, EventArgs e)
        {
            Form_send.tmpCookies = new CookieCollection();

            string temp = "";
            foreach (Cookie c in Form_send.tmpCookies)
            {
                temp += c.ToString();
            }

            this.richTextBox1.Text = temp;

        }

        /// <summary>
        /// cha kan cookie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click_1(object sender, EventArgs e)
        {
            string temp = "";
            foreach (Cookie c in Form_send.tmpCookies) {
                temp += c.ToString()+"\n";
            }

            this.richTextBox1.Text = temp;
        }
    }
}